package com.example.wlt.personnel_manegement_system.Adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.wlt.personnel_manegement_system.Item.Item_audit;
import com.example.wlt.personnel_manegement_system.R;

import java.util.List;

/**
 * Created by WLT on 2016/5/18.
 */
public class AuditAdapter extends BaseAdapter {
    private List<Item_audit> mList;
    private LayoutInflater mInflater;
    private Activity activity;
    public AuditAdapter(Activity activity,Context context, List<Item_audit> List){
        mList=List;
        mInflater=LayoutInflater.from(context);
        this.activity=activity;
    }
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView == null){
            holder=new ViewHolder();
            convertView=mInflater.inflate(R.layout.organization_audit_item,null);
            holder.name=(TextView)convertView.findViewById(R.id.tv_organization_name);
            holder.department=(TextView)convertView.findViewById(R.id.tv_organization_department);
            holder.agree=(TextView)convertView.findViewById(R.id.tv_organization_agree);
            holder.refuse=(TextView)convertView.findViewById(R.id.tv_organization_refuse);
            convertView.setTag(holder);
        }else {
            holder=(ViewHolder)convertView.getTag();
        }

       Item_audit mItem=mList.get(position);

        holder.name.setText(mItem.Itemname);
        holder.department.setText(mItem.Itemdepartment);
        holder.agree.setText(mItem.Itemagree);
        holder.refuse.setText(mItem.Itemrefuse);

        final ViewHolder finalHolder = holder;
        holder.agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(position);
            }
        });
        final ViewHolder finalHolder1 = holder;
        holder.refuse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog1(position);
            }
        });
        return convertView;
    }
    //对话框
    private void showDialog(final int position){
        AlertDialog.Builder builder=new AlertDialog.Builder(activity);
        builder.setMessage("您将同意该同学加入部门");
        builder.setNegativeButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mList.remove(position);
                notifyDataSetChanged();
            }
        });
        builder.setPositiveButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        AlertDialog dialog=builder.create();
        dialog.show();
    }
    //对话框
    private void showDialog1(final int position){
        AlertDialog.Builder builder=new AlertDialog.Builder(activity);
        builder.setMessage("您将拒绝该同学加入部门");
        builder.setNegativeButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mList.remove(position);
                notifyDataSetChanged();
            }
        });
        builder.setPositiveButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        AlertDialog dialog=builder.create();
        dialog.show();
    }
    class ViewHolder{
        protected TextView name;
        protected TextView department;
        protected TextView agree;
        protected TextView refuse;
    }
}
